public class Node{
  
  ArrayList<Polygon> figures;
  PVector origin;
  float angle;
  Node parent;
  ArrayList<Node> childs; 
  
  public Node(ArrayList<Polygon> figures,PVector origin, float rotationAngle,Node parent, ArrayList<Node> childs){
    this.figures = figures;
    this.origin = origin;
    this.angle = rotationAngle;
    this.parent = parent;
    this.childs = childs; 
  }
  
  public Node(Node parent,PVector origin, float rotationAngle){
    this.figures = new ArrayList<Polygon>();
    this.childs = new ArrayList<Node>();
    this.parent = parent;
    if(parent!=null)parent.addChild(this);
    this.origin = origin;
    this.angle = rotationAngle;
  }
  public Node(PVector origin, float rotationAngle){
    this(null,origin,rotationAngle);
  }
  
  public void addFigure(Polygon p){
    this.figures.add(p);
  }
  
  public void transformWorld(PGraphics plot){
    plot.translate(origin.x,origin.y);
    plot.rotate(angle);
  }
  
  public void addChild(Node child){
    childs.add(child);
  }
  
  public void setParent(Node newParent){
    this.parent = newParent;
  }
  
  public Node getParent(){
    return parent;
  }
  
  private void drawAxis(PGraphics plot){
    plot.line(0,0,0,10);
    plot.line(0,0,10,0);
  }
  
  public void setAngle(float newAngle){
    this.angle = newAngle;
  }
  
  public void setOrigin(PVector newOrigin){
    this.origin = newOrigin;
  }
  
  public PVector getOrigin(){
    return this.origin;
  }
 
 public void draw(PGraphics plot){
   plot.pushMatrix();
   this.transformWorld(plot);
   //this.drawAxis(plot);
   for(int i=0; i<figures.size(); i++){
     figures.get(i).draw(plot);
   }
   for(int i=0; i < childs.size(); i++){
     childs.get(i).draw(plot);
   }
   plot.popMatrix();
 }
 
 public ArrayList<Polygon> getFigures(){
   Matrix4 m = Matrix4.translation(origin.x,origin.y).multiplyMatrix(Matrix4.rotation(angle));
   ArrayList<Polygon> ret =new ArrayList<Polygon>();
   for(int i=0; i < figures.size(); i++){
     ret.add(figures.get(i).clone());
   }
   for(int i=0; i<childs.size(); i++){
     ret.addAll(childs.get(i).getFigures());
   }
   for(int i=0; i<ret.size(); i++){
     ret.get(i).transform(m);
   }
   return ret;
 }
 
  
}
